home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / boot / scsiDiskBoot / sun3.md / devSCSI3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-13  |  60.6 KB  |  1,937 lines

  1. /* 
  2.  * devSCSI3.c --
  3.  *
  4.  *    Routines specific to the SCSI-3 Host Adaptor.  This adaptor is
  5.  *    based on the NCR 5380 chip.  There are two variants, one is
  6.  *    "onboard" the main CPU board (3/50, 3/60, 4/110) and uses a
  7.  *    Universal DMA Controller chip, the AMD 9516 UDC.  The other is
  8.  *    on the VME and has a much simpler DMA interface.  Both are
  9.  *    supported here.  The 5380 supports connect/dis-connect.
  10.  *
  11.  * Copyright 1988 Regents of the University of California
  12.  * Permission to use, copy, modify, and distribute this
  13.  * software and its documentation for any purpose and without
  14.  * fee is hereby granted, provided that the above copyright
  15.  * notice appear in all copies.  The University of California
  16.  * makes no representations about the suitability of this
  17.  * software for any purpose.  It is provided "as is" without
  18.  * express or implied warranty.
  19.  */
  20.  
  21. #ifdef notdef
  22. static char rcsid[] = "$Header: /sprite/src/kernel/dev/sun3.md/RCS/devSCSI3.c,v 8.8 89/05/24 07:51:33 rab Exp $ SPRITE (Berkeley)";
  23. #endif /* not lint */
  24.  
  25. #include "sprite.h"
  26. #include "../sync.h"
  27. #include "scsi3.h"
  28. #include "mach.h"
  29. #include "dev.h"
  30. #include "devInt.h"
  31. #include "scsiHBA.h"
  32. #include "scsiDevice.h"
  33. #include "stdlib.h"
  34. #include "boot.h"
  35.  
  36. #include "dbg.h"
  37. #ifdef SCSI3_BOOT
  38. /*
  39.  *
  40.  *      Definitions for Sun's second variant on the SCSI device interface.
  41.  *    This interface is  found on 32-bit VME versions, i.e. some plug-in
  42.  *    controllers, and the 3/{56}0.  The associated paper reference is
  43.  *    "Hardware Reference Manual for the Sun-3 SCSI Board".  This explains
  44.  *    general behavior of the VME version of the SCSI-3 interface.
  45.  *    The reference for the 5380 is the NCR Standard Products Data Book,
  46.  *    Micro-electronics Division.  Page number references refer to the
  47.  *    4/88 (April 88) edition.  The UDC (Universal DMA Controller) chip
  48.  *    is the AMD 9516 and the AMD reference manual can be consulted for chip
  49.  *    specifics.
  50.  *
  51.  *
  52.  *    The 5380 has 8 general registers.  They have different functions
  53.  *    when read and written.  See pp. 80-85 for register descriptions.
  54.  *    This chip allows direct control over the SCSI bus by the CPU,
  55.  *    so many bits correspond directly to SCSI bus signals.
  56.  */
  57. typedef struct ReadRegs {
  58.     unsigned char data;        /* Data register.  A direct connection to
  59.                  * the SCSI data bus.  This is read during
  60.                  * programmed I/O to get msgs, and during
  61.                  * arbitration. */
  62.     unsigned char initCmd;    /* Initiator command register */
  63.     unsigned char mode;        /* Mode register */
  64.     unsigned char trgtCmd;    /* Target command register */
  65.     unsigned char curStatus;    /* All SCSI signals except ATN and ACK */
  66.     unsigned char status;    /* ATN, ACK, plus DMA and interrupt signals */
  67.     unsigned char inData;    /* Input data register.  Used for "latched"
  68.                  * data on the SCSI bus during DMA.  This
  69.                  * is not accessed directly by the driver. */
  70.     unsigned char clear;    /* Read this to clear the following bits
  71.                  * in the status register: parity error,
  72.                  * interrupt request, busy failure. */
  73. } ReadRegs;
  74.  
  75. /*
  76.  * The following format applies when writing the registers.
  77.  */
  78. typedef struct WriteRegs {
  79.     unsigned char data;        /* Data register.  Contains the ID of the
  80.                  * SCSI "target", or controller, for the 
  81.                  * SELECT phase. Also, leftover odd bytes
  82.                  * are left here after a read. */
  83.     unsigned char initCmd;    /* Initiator command register */
  84.     unsigned char mode;        /* Mode register */
  85.     unsigned char trgtCmd;    /* Target command register */
  86.     unsigned char select;    /* Select/reselect enable register */
  87.     /*
  88.      * DMA is initiated by writing to these registers.  The TARGET mode
  89.      * bit should be set right, i.e. cleared before writing to
  90.      * the send or initRecv registers, and the DMA mode bit should be set.
  91.      */
  92.     unsigned char send;        /* Start DMA from memory to SCSI bus */
  93.     unsigned char trgtRecv;    /* Start DMA from SCSI bus to target */
  94.     unsigned char initRecv;    /* Start DMA from SCSI bus to initiator */
  95. } WriteRegs;
  96.  
  97. /*
  98.  * Control bits in the 5380 Initiator Command Register.
  99.  * RST, ACK, BSY, SEL, ATN are direct connections to SCSI control lines.
  100.  * Setting or clearing the bit raises or lowers the SCSI signal.
  101.  * Reading these bits indicates the current value of the control signal.
  102.  */
  103. #define    SBC_ICR_RST    0x80    /* (r/w) SCSI RST (reset) signal */
  104. #define SBC_ICR_AIP    0x40    /* (r)   arbitration in progress */
  105. #define SBC_ICR_TEST    0x40    /* (w)   test mode, disables output */
  106. #define SBC_ICR_LA    0x20    /* (r)   lost arbitration */
  107. #define SBC_ICR_DE    0x20    /* (w)   differential enable (5381 only) */
  108. #define SBC_ICR_ACK    0x10    /* (r/w) SCSI ACK (acknowledge) signal */
  109. #define SBC_ICR_BUSY    0x08    /* (r/w) SCSI BSY (busy) signal */
  110. #define SBC_ICR_SEL    0x04    /* (r/w) SCSI SEL (select) signal */
  111. #define SBC_ICR_ATN    0x02    /* (r/w) SCSI ATN (attention) signal */
  112. #define SBC_ICR_DATA    0x01    /* (r/w) assert data bus.  Enables the outData
  113.                  * contents to be output on the SCSi data lines.
  114.                  * This should be set during DMA send. */
  115.  
  116. /*
  117.  * Bits in the 5380 Mode Register (same on read or write).
  118.  * "This is used to control the operation of the chip."
  119.  * The mode controls DMA, target/initiator roles, parity, and interrupts.
  120.  */
  121. #define SBC_MR_BDMA    0x80    /* Enable block mode dma */
  122. #define SBC_MR_TRG    0x40    /* Target mode when set, else Initiator */
  123. #define SBC_MR_EPC    0x20    /* Enable parity check */
  124. #define SBC_MR_EPI    0x10    /* Enable parity interrupt */
  125. #define SBC_MR_EEI    0x08    /* Enable eop (end-of-process, dma) interrupt */
  126. #define SBC_MR_MBSY    0x04    /* Enable monitoring of BSY (busy) signal */
  127. #define SBC_MR_DMA    0x02    /* Enable DMA.  Used with other DMA regs. */
  128. #define SBC_MR_ARB    0x01    /* Set during SCSI bus arbitration */
  129.  
  130. /*
  131.  * Bits in the 5380 Target Command Register.
  132.  * As an Initator, which we always are, this register must be set to
  133.  * match the current phase that's on the SCSI bus before sending data.
  134.  */
  135. #define SBC_TCR_REQ    0x08    /* assert request.  Only for targets. */
  136. #define SBC_TCR_MSG    0x04    /* message phase, if set */
  137. #define SBC_TCR_CD    0x02    /* command phase if set, else data phase */
  138. #define SBC_TCR_IO    0x01    /* input phase if set, else output */
  139.  
  140. /*
  141.  * Combinations for different phases as represented in the target cmd. reg.
  142.  */
  143. #define TCR_COMMAND    (SBC_TCR_CD)
  144. #define TCR_STATUS    (SBC_TCR_CD | SBC_TCR_IO)
  145. #define TCR_MSG_OUT    (SBC_TCR_MSG | SBC_TCR_CD)
  146. #define TCR_MSG_IN    (SBC_TCR_MSG | SBC_TCR_CD | SBC_TCR_IO)
  147. #define TCR_DATA_OUT    0
  148. #define TCR_DATA_IN    (SBC_TCR_IO)
  149. #define TCR_UNSPECIFIED    (SBC_TCR_MSG)
  150.  
  151. /*
  152.  * Bits in the 5380 Current SCSI Bus Status register (read only).
  153.  * This register is used to monitor the current state of all of
  154.  * the SCSI bus lines except ATN (attention) and ACK (acknowledge).
  155.  */
  156. #define SBC_CBSR_RST    0x80    /* reset */
  157. #define SBC_CBSR_BSY    0x40    /* busy */
  158. #define SBC_CBSR_REQ    0x20    /* request */
  159. #define SBC_CBSR_MSG    0x10    /* message */
  160. #define SBC_CBSR_CD    0x08    /* command/data */
  161. #define SBC_CBSR_IO    0x04    /* input/output */
  162. #define SBC_CBSR_SEL    0x02    /* select */
  163. #define SBC_CBSR_DBP    0x01    /* data bus parity */
  164.  
  165. /*
  166.  * Combinations for different phases as represented on the SCSI bus.
  167.  * COMMAND phase is used to send a command block to a Target.
  168.  * STATUS phase is used to get status bytes from a Target.
  169.  * MSG_OUT phase is used to send message bytes to a Target.
  170.  * MSG_IN phase is used to get a message from a Target.
  171.  * DATA_OUT phase is used to send data to a Target.
  172.  * DATA_IN phase is used when receiving data from a Target.
  173.  */
  174. #define CBSR_PHASE_BITS    (SBC_CBSR_CD | SBC_CBSR_MSG | SBC_CBSR_IO)
  175. #define PHASE_COMMAND    (SBC_CBSR_CD)
  176. #define PHASE_STATUS    (SBC_CBSR_CD | SBC_CBSR_IO)
  177. #define PHASE_MSG_OUT    (SBC_CBSR_MSG | SBC_CBSR_CD)
  178. #define PHASE_MSG_IN    (SBC_CBSR_MSG | SBC_CBSR_CD | SBC_CBSR_IO)
  179. #define PHASE_DATA_OUT    0
  180. #define PHASE_DATA_IN    (SBC_CBSR_IO)
  181.  
  182. /*
  183.  * Bits in the 5380 Bus and Status register.  This has the ATN and ACK
  184.  * SCSI lines, plus other status bits.
  185.  */
  186. #define SBC_BSR_EDMA    0x80    /* End of dma, almost, see p. 84 */
  187. #define SBC_BSR_RDMA    0x40    /* DRQ (dma request) signal, set during DMA */
  188. #define SBC_BSR_PERR    0x20    /* Parity error */
  189. #define SBC_BSR_INTR    0x10    /* IRQ (interrupt request) */
  190. #define SBC_BSR_PMTCH    0x08    /* Phase match indicates if trgtCmd is ok */
  191. #define SBC_BSR_BERR    0x04    /* Busy error set when BSY goes away */
  192. #define SBC_BSR_ATN    0x02    /* SCSI ATN (attention) signal */
  193. #define SBC_BSR_ACK    0x01    /* SCSI ACK (acknowledge)_signal */
  194.  
  195. /*
  196.  * AMD 9516 UDC (Universal DMA Controller) Registers.
  197.  * Sun3/50 and Sun3/60.
  198.  */
  199.  
  200. /* addresses of the udc registers accessed directly by driver */
  201. #define UDC_ADR_MODE        0x38    /* master mode register */
  202. #define UDC_ADR_COMMAND        0x2e    /* command register (write only) */
  203. #define UDC_ADR_STATUS        0x2e    /* status register (read only) */
  204. #define UDC_ADR_CAR_HIGH    0x26    /* chain addr reg, high word */
  205. #define UDC_ADR_CAR_LOW        0x22    /* chain addr reg, low word */
  206. #define UDC_ADR_CARA_HIGH    0x1a    /* cur addr reg A, high word */
  207. #define UDC_ADR_CARA_LOW    0x0a    /* cur addr reg A, low word */
  208. #define UDC_ADR_CARB_HIGH    0x12    /* cur addr reg B, high word */
  209. #define UDC_ADR_CARB_LOW    0x02    /* cur addr reg B, low word */
  210. #define UDC_ADR_CMR_HIGH    0x56    /* channel mode reg, high word */
  211. #define UDC_ADR_CMR_LOW        0x52    /* channel mode reg, low word */
  212. #define UDC_ADR_COUNT        0x32    /* number of words to transfer */
  213.  
  214. /* 
  215.  * For a dma transfer, the appropriate udc registers are loaded from a 
  216.  * table in memory pointed to by the chain address register.
  217.  */
  218. typedef struct UDCDMAtable {
  219.     unsigned short    rsel;    /* tells udc which regs to load */
  220.     unsigned short    haddr;    /* high word of main mem dma address */
  221.     unsigned short    laddr;    /* low word of main mem dma address */
  222.     unsigned short    count;    /* num words to transfer */
  223.     unsigned short    hcmr;    /* high word of channel mode reg */
  224.     unsigned short    lcmr;    /* low word of channel mode reg */
  225. } UDCDMAtable;
  226.  
  227. /* indicates which udc registers are to be set based on info in above table */
  228. #define UDC_RSEL_RECV        0x0182
  229. #define UDC_RSEL_SEND        0x0282
  230.  
  231. /* setting of chain mode reg: selects how the dma op is to be executed */
  232. #define UDC_CMR_HIGH        0x0040    /* high word of channel mode reg */
  233. #define UDC_CMR_LSEND        0x00c2    /* low word of cmr when send */
  234. #define UDC_CMR_LRECV        0x00d2    /* low word of cmr when receiving */
  235.  
  236. /* setting for the master mode register */
  237. #define UDC_MODE        0xd    /* enables udc chip */
  238.  
  239. /* setting for the low byte in the high word of an address */
  240. #define UDC_ADDR_INFO        0x40    /* inc addr after each word is dma'd */
  241.  
  242. /* udc commands */
  243. #define UDC_CMD_STRT_CHN    0xa0    /* start chaining */
  244. #define UDC_CMD_CIE        0x32    /* channel 1 interrupt enable */
  245. #define UDC_CMD_RESET        0x00    /* reset udc, same as hdw reset */
  246.  
  247. /* bits in the udc status register */
  248. #define UDC_SR_CIE        0x8000    /* channel interrupt enable */
  249. #define UDC_SR_IP        0x2000    /* interrupt pending */
  250. #define UDC_SR_CA        0x1000    /* channel abort */
  251. #define UDC_SR_NAC        0x0800    /* no auto reload or chaining*/
  252. #define UDC_SR_WFB        0x0400    /* waiting for bus */
  253. #define UDC_SR_SIP        0x0200    /* second interrupt pending */
  254. #define UDC_SR_HM        0x0040    /* hardware mask */
  255. #define UDC_SR_HRQ        0x0020    /* hardware request */
  256. #define UDC_SR_MCH        0x0010    /* match on upper comparator byte */
  257. #define UDC_SR_MCL        0x0008    /* match on lower comparator byte */
  258. #define UDC_SR_MC        0x0004    /* match condition ended dma */
  259. #define UDC_SR_EOP        0x0002    /* eop condition ended dma */
  260. #define UDC_SR_TC        0x0001    /* termination of count ended dma */
  261.  
  262. /*
  263.  * Misc defines 
  264.  */
  265. /*
  266.  * Values for the reset argument of WaitReg.
  267.  */
  268. #define RESET        TRUE
  269. #define NO_RESET    FALSE
  270.  
  271. /* arbitrary retry count */
  272. #define SI_NUM_RETRIES        2
  273. /*
  274.  * WAIT_LENGTH - the number of microseconds that the host waits for
  275.  *    various control lines to be set on the SCSI bus.  The largest wait
  276.  *    time is when a controller is being selected.  This delay is
  277.  *    called the Bus Abort delay and is about 250 milliseconds.
  278.  */
  279.  
  280. #define    WAIT_LENGTH        250000
  281.  
  282. /* scsi timer values, all in microseconds */
  283. #define SI_ARBITRATION_DELAY    3
  284. #define SI_BUS_CLEAR_DELAY    1
  285. #define SI_BUS_SETTLE_DELAY    1
  286. #define SI_UDC_WAIT        1
  287. #define    SI_WAIT_COUNT        250000
  288.  
  289. /* directions for dma transfers */
  290. #define SI_RECV_DATA        0
  291. #define SI_SEND_DATA        1
  292. #define SI_NO_DATA        2
  293.  
  294. /* initiator's scsi device id */
  295. #define    SI_HOST_ID        0x80
  296. /*
  297.  * INTR_ADDR(vector) - Compute the correct interruptAddr modifier
  298.  * for the VME version of this interface. Vector is the VME interrupt
  299.  * to use.  The high order 8 bits is the address space modiifer.
  300.  * The correct value 0x3d - 24 bit address Supervisor data space. 
  301.  * We shift it right 8 bits to leave room the the interrupt vector
  302.  * we or in.
  303.  */
  304. #define INTR_ADDR(vector)    ((0x3d<<8)|(vector))
  305.  
  306. /*
  307.  * Maximum data transfer size for the onBoard HBA appears to be
  308.  * limited by the 16 bit dma register. Since this counter is in 
  309.  * words we can send up to 127 K but not 128 K. The DMA counter
  310.  * on the VME version is 24 bits.  As it turns out both this values are
  311.  * limited by the size of the mapped DMA buffer DEV_MAX_TRANSFER_SIZE.
  312.  */
  313.  
  314. #define    MAX_ONBOARD_TRANSFER_SIZE    (64*1024)
  315.  
  316. #define    MAX_VME_TRANSFER_SIZE        (64*1024)
  317.  
  318. /* 
  319.  * Register layout for the SCSI control logic interface.
  320.  * Some of these registers apply to only one interface and some
  321.  * apply to both. The registers which apply to the Sun3/50 onboard 
  322.  * version only are udc_rdata and udc_raddr. The registers which
  323.  * apply to the Sun3 vme version only are dma_addr, dma_count, bpr,
  324.  * iv_am, and bcrh. Thus, the sbc registers, fifo_data, bcr, and csr 
  325.  * apply to both interfaces.
  326.  * One other feature of the vme interface: a write to the dma count 
  327.  * register also causes a write to the fifo byte count register and
  328.  * vice-versa.
  329.  */
  330. typedef struct CtrlRegs {
  331.     union {
  332.         struct ReadRegs    read;    /* scsi bus ctrl, read reg */
  333.         struct WriteRegs    write;    /* scsi bus ctrl, write reg */
  334.     } sbc;                    /* SBC 5380 registers, 8 bytes*/
  335.     unsigned short        dmaAddressHigh;    /* dma address register High */
  336.     unsigned short        dmaAddressLow;    /* dma address register Low */
  337.     unsigned short        dmaCountHigh;    /* dma count register High */
  338.     unsigned short        dmaCountLow;    /* dma count register low*/
  339.     unsigned short        udcRdata;    /* UDC, reg data */
  340.     unsigned short        udcRaddr;    /* UDC, reg addr */
  341.     unsigned short        fifoData;    /* fifo data register */
  342.                         /* holds extra byte on odd */
  343.                         /* byte dma read */
  344.     unsigned short        fifoCountLow;    /* fifo byte count reg */
  345.     unsigned short        control;    /* control/status register */
  346.     unsigned short        bytePackHigh;    /* Byte 0 and Byte 1 */
  347.     unsigned short        bytePackLow;    /* Byte 2 and Byte 3 */
  348.     unsigned short        addrIntr;    /* bits 0-7: addr modifier */
  349.                         /* bits 8-13: intr vector */
  350.                         /* bits 14-15: unused */
  351.     unsigned short        fifoCountHigh;    /* high portion of fifoCount */
  352. } CtrlRegs;
  353.  
  354. /*
  355.  * The dmaAddress, dmaCount, and fifoCount are 32 or 24 bit registers with only
  356.  * a 16 bit path to them. Because of this we have the following macros
  357.  * for setting and reading them. 
  358.  * SET_FIFO_COUNT()    - Set the FIFO count register to the provided value.
  359.  * READ_FIFO_COUNT()    - Set the FIFO count register.
  360.  * SET_DMA_COUNT()    - Set the DMA count register to the provided value.
  361.  * READ_DMA_COUNT()    - Read the DMA count register.
  362.  * SET_DMA_ADDR()    - Set the DMA address register to the provided value.
  363.  * READ_DMA_ADDR()    - Read the DMA address register.
  364.  */
  365. #ifdef bigio_works
  366. #define    SET_FIFO_COUNT(regsPtr, value)    {\
  367.         (regsPtr)->fifoCountLow = ((unsigned) (value) & 0xffff);          \
  368.         (regsPtr)->fifoCountHigh = (((unsigned)(value) >> 16) & 0xff);    \
  369.     }
  370. #define    READ_FIFO_COUNT(regsPtr)    \
  371.        ((((regsPtr)->fifoCountHigh&0xff) << 16) | ((regsPtr)->fifoCountLow))
  372.  
  373. #define    SET_DMA_COUNT(regsPtr, value)    {\
  374.         (regsPtr)->dmaCountLow = ((unsigned) (value) & 0xffff);          \
  375.         (regsPtr)->dmaCountHigh = (((unsigned)(value) >> 16) & 0xff);    \
  376.         }
  377. #define    READ_DMA_COUNT(regsPtr)    \
  378.         ((((regsPtr)->dmaCountHigh&0xff) << 16) | ((regsPtr)->dmaCountLow))
  379. #else
  380. #define    SET_FIFO_COUNT(regsPtr, value)    {\
  381.         (regsPtr)->fifoCountLow = ((unsigned) (value) & 0xffff);          \
  382.         (regsPtr)->fifoCountHigh = (((unsigned)(value) >> 16) & 0x0);    \
  383.     }
  384. #define    READ_FIFO_COUNT(regsPtr)    \
  385.        ( ((regsPtr)->fifoCountLow))
  386.  
  387. #define    SET_DMA_COUNT(regsPtr, value)    {\
  388.         (regsPtr)->dmaCountLow = ((unsigned) (value) & 0xffff);          \
  389.         (regsPtr)->dmaCountHigh = (((unsigned)(value) >> 16) & 0x0);    \
  390.         }
  391. #define    READ_DMA_COUNT(regsPtr)    \
  392.         (((regsPtr)->dmaCountLow))
  393. #endif
  394. #define    SET_DMA_ADDR(regsPtr, value)    {\
  395.         (regsPtr)->dmaAddressLow = ((unsigned) (value) & 0xffff);          \
  396.         (regsPtr)->dmaAddressHigh = (((unsigned)(value) >> 16) & 0xffff); \
  397.     }
  398. #define    READ_DMA_ADDR(regsPtr)    \
  399.         (((regsPtr)->dmaAddressHigh << 16) | ((regsPtr)->dmaAddressLow))
  400.  
  401. /*
  402.  * Status Register.
  403.  * Note:
  404.  *    (r)    indicates bit is read only.
  405.  *    (rw)    indicates bit is read or write.
  406.  *    (v)    vme host adaptor interface only.
  407.  *    (o)    sun3/50 onboard host adaptor interface only.
  408.  *    (b)    both vme and sun3/50 host adaptor interfaces.
  409.  */
  410. #define SI_CSR_DMA_ACTIVE    0x8000    /* (r,o) dma transfer active */
  411. #define SI_CSR_DMA_CONFLICT    0x4000    /* (r,b) reg accessed while dmaing */
  412. #define SI_CSR_DMA_BUS_ERR    0x2000    /* (r,b) bus error during dma */
  413. #define SI_CSR_ID        0x1000    /* (r,b) 0 for 3/50, 1 for SCSI-3, */
  414.                     /* 0 if SCSI-3 unmodified */
  415. #define SI_CSR_FIFO_FULL    0x0800    /* (r,b) fifo full */
  416. #define SI_CSR_FIFO_EMPTY    0x0400    /* (r,b) fifo empty */
  417. #define SI_CSR_SBC_IP        0x0200    /* (r,b) sbc interrupt pending */
  418. #define SI_CSR_DMA_IP        0x0100    /* (r,b) dma interrupt pending */
  419. #define SI_CSR_LOB        0x00c0    /* (r,v) number of leftover bytes */
  420. #define SI_CSR_LOB_THREE    0x00c0    /* (r,v) three leftover bytes */
  421. #define SI_CSR_LOB_TWO        0x0080    /* (r,v) two leftover bytes */
  422. #define SI_CSR_LOB_ONE        0x0040    /* (r,v) one leftover byte */
  423. #define SI_CSR_BPCON        0x0020    /* (rw,v) byte packing control */
  424.                     /* dma is in 0=longwords, 1=words */
  425. #define SI_CSR_DMA_EN        0x0010    /* (rw,v) dma enable */
  426. #define SI_CSR_SEND        0x0008    /* (rw,b) dma dir, 1=to device */
  427. #define SI_CSR_INTR_EN        0x0004    /* (rw,b) interrupts enable */
  428. #define SI_CSR_FIFO_RES        0x0002    /* (rw,b) inits fifo, 0=reset */
  429. #define SI_CSR_SCSI_RES        0x0001    /* (rw,b) reset sbc and udc, 0=reset */
  430.  
  431.  
  432.  
  433. /*
  434.  * Number of times to try things like target selection.
  435.  */
  436. #define SBC_NUM_RETRIES 3
  437.  
  438. /*
  439.  * Registers are passed into the wait routine as Addresses, and their
  440.  * size is passed in as a separate argument to determine type coercion.
  441.  */
  442. typedef enum {
  443.     REG_BYTE,
  444.     REG_SHORT
  445. } RegType;
  446.  
  447. /*
  448.  * For waiting, there are several possibilities:
  449.  *  ACTIVE_HIGH - wait for any bits in mask to be 1
  450.  *  ACTIVE_ALL  - wait for all bits in mask to be 1
  451.  *  ACTIVE_LOW  - wait for any bits in mask to be 0.
  452.  *  ACTIVE_NONE - wait for all bits in mask to be 0.
  453.  */
  454. typedef enum {
  455.     ACTIVE_HIGH,
  456.     ACTIVE_ALL,
  457.     ACTIVE_LOW,
  458.     ACTIVE_NONE,
  459. } BitSelection;        
  460.  
  461. /* Forward declaration. */
  462. typedef struct Controller Controller;
  463.  
  464. /*
  465.  * Device - The data structure containing information about a device. One of
  466.  * these structure is kept for each attached device. Note that is structure
  467.  * is casted into a ScsiDevice and returned to higher level software.
  468.  * This implies that the ScsiDevice must be the first field in this
  469.  * structure.
  470.  */
  471.  
  472. typedef struct Device {
  473.     ScsiDevice handle;    /* Scsi Device handle. This is the only part
  474.              * of this structure visible to higher 
  475.              * level software. MUST BE FIRST FIELD IN STRUCTURE.
  476.              */
  477.     int    targetID;    /* SCSI Target ID of this device. Note that
  478.              * the LUN is store in the device handle.  */
  479.     Controller *ctrlPtr;    /* Controller to which device is attached. */
  480.            /*
  481.             * The following part of this structure is 
  482.             * used to handle SCSI commands that return 
  483.             * CHECK status. To handle the REQUEST SENSE
  484.             * command we must: 1) Save the state of the current
  485.             * command into the "struct FrozenCommand". 2) Submit
  486.             * a request sense command formatted in SenseCmd
  487.             * to the device.  */
  488.     struct FrozenCommand {               
  489.     ScsiCmd    *scsiCmdPtr;       /* The frozen command. */
  490.     unsigned char statusByte; /* It's SCSI status byte, Will always have
  491.                    * the check bit set.
  492.                    */
  493.     int amountTransferred;    /* Number of bytes transferred by this 
  494.                    * command.
  495.                    */
  496.     } frozen;    
  497.     char senseBuffer[DEV_MAX_SENSE_BYTES]; /* Data buffer for request sense */
  498.     ScsiCmd        SenseCmd;         /* Request sense command buffer. */
  499. } Device;
  500.  
  501. /*
  502.  * Controller - The Data structure describing a sun SCSI3 controller. One
  503.  * of these structures exists for each active SCSI3 HBA on the system. Each
  504.  * controller may have from zero to 56 (7 targets each with 8 logical units)
  505.  * devices attached to it. 
  506.  */
  507. struct Controller {
  508.     volatile CtrlRegs *regsPtr; /* Pointer to the registers of
  509.                                     this controller. */
  510.     Boolean  onBoard;    /* TRUE if this is a on board version of the 
  511.              * controller such as in the Sun 3/50, 3/60. FALSE
  512.              * if it is the VME version.
  513.              */
  514.     UDCDMAtable    *udcDmaTable; /* Table for the onBoard's DMA chip. */
  515.     int        dmaState;    /* DMA state for this controller, defined below. */
  516.     Boolean dmaSetup;    /* TRUE if the DMA register have been setup. Only used
  517.              * for the VME version. */
  518.     char    *name;    /* String for error message for this controller.  */
  519.     Sync_Semaphore mutex; /* Lock protecting controller's data structures. */
  520.               /* Until disconnect/reconnect is added we can have
  521.                * only one current active device and scsi command.*/
  522.     Device     *devPtr;       /* Current active command. */
  523.     ScsiCmd   *scsiCmdPtr; /* Current active command. */
  524.     Address    dmaBuffer; /* DMA buffer allocated for this address. */
  525.     Device  *devicePtr; /* Pointers to the device attached to the 
  526.                    * controller index by [targetID][LUN].
  527.                    * NIL if device not attached yet. Zero if
  528.                    * device conflicts with HBA address.  */
  529. };
  530.  
  531. /*
  532.  * Possible values for the dmaState state field of a controller.
  533.  *
  534.  * DMA_RECEIVE  - data is being received from the device, such as on
  535.  *    a read, inquiry, or request sense.
  536.  * DMA_SEND     - data is being send to the device, such as on a write.
  537.  * DMA_INACTIVE - no data needs to be transferred.
  538.  */
  539.  
  540. #define DMA_RECEIVE  0x0
  541. #define    DMA_SEND     0x2
  542. #define    DMA_INACTIVE 0x4
  543.  
  544. /*
  545.  * Test, mark, and unmark the controller as busy.
  546.  */
  547. #define    IS_CTRL_BUSY(ctrlPtr)    ((ctrlPtr)->scsiCmdPtr != (ScsiCmd *) NIL)
  548. #define    SET_CTRL_BUSY(ctrlPtr,scsiCmdPtr) \
  549.         ((ctrlPtr)->scsiCmdPtr = (scsiCmdPtr))
  550. #define    SET_CTRL_FREE(ctrlPtr)    ((ctrlPtr)->scsiCmdPtr = (ScsiCmd *) NIL)
  551.  
  552. /*
  553.  * MAX_SCSI3_CTRLS - Maximum number of SCSI3 controllers attached to the
  554.  *             system. We set this to the maximum number of VME slots
  555.  *             in any Sun system currently available.
  556.  */
  557. #define    MAX_SCSI3_CTRLS    1
  558. static Controller *Controllers[MAX_SCSI3_CTRLS];
  559. /*
  560.  * Highest number controller we have probed for.
  561.  */
  562. static int numSCSI3Controllers = 0;
  563.  
  564. /*
  565.  * Forward declarations.  
  566.  */
  567.  
  568. static void        Reset();
  569. static ReturnStatus    SendCommand();
  570. static ReturnStatus    GetStatusByte();
  571. static ReturnStatus    WaitPhase();
  572. static ReturnStatus    WaitReg();
  573. static ReturnStatus GetByte();
  574. static ReturnStatus PutByte();
  575. static void     PrintRegs();
  576. static void     StartDMA();
  577.  
  578.  
  579.  
  580. /*
  581.  *----------------------------------------------------------------------
  582.  *
  583.  * Reset --
  584.  *
  585.  *    Reset a SCSI bus controlled by the SCSI-3 Sun Host Adaptor.
  586.  *
  587.  * Results:
  588.  *    None.
  589.  *
  590.  * Side effects:
  591.  *    Reset the controller and SCSI bus.
  592.  *
  593.  *----------------------------------------------------------------------
  594.  */
  595. static void
  596. Reset(ctrlPtr)
  597.     Controller *ctrlPtr;
  598. {
  599.     volatile CtrlRegs *regsPtr = (volatile CtrlRegs *)ctrlPtr->regsPtr;
  600.     unsigned char clear;
  601.  
  602.     SET_FIFO_COUNT(regsPtr,0);
  603.     regsPtr->control = 0;
  604.     MACH_DELAY(100);
  605.     regsPtr->control = SI_CSR_SCSI_RES | SI_CSR_FIFO_RES;
  606.  
  607. #ifndef SCSI3_ONBOARD
  608.     SET_DMA_COUNT(regsPtr,0);
  609.     SET_DMA_ADDR(regsPtr,0);
  610. #endif
  611.     regsPtr->sbc.write.initCmd = SBC_ICR_RST;
  612.     MACH_DELAY(1000);
  613.     regsPtr->sbc.write.initCmd = 0;
  614.     
  615.     clear = regsPtr->sbc.read.clear;
  616. #ifdef lint
  617.     regsPtr->sbc.read.clear = clear;
  618. #endif
  619.     regsPtr->control |= SI_CSR_INTR_EN;
  620.     regsPtr->sbc.write.mode = 0;
  621. }
  622.  
  623.  
  624. /*
  625.  *----------------------------------------------------------------------
  626.  *
  627.  * SendCommand --
  628.  *
  629.  *      Send a command to a SCSI controller via the SCSI-3 Host Adaptor.
  630.  *    NOTE: The caller is assumed to have the master lock of the controller
  631.  *    to which the device is attached held.
  632.  *      
  633.  *
  634.  * Results:
  635.  *    An error code.
  636.  *
  637.  * Side effects:
  638.  *    Those of the command (Read, write etc.)
  639.  *
  640.  *----------------------------------------------------------------------
  641.  */
  642. static ReturnStatus
  643. SendCommand(devPtr, scsiCmdPtr)
  644.     Device     *devPtr;        /* Device to sent to. */
  645.     ScsiCmd    *scsiCmdPtr;        /* Command to send. */
  646. {
  647.     ReturnStatus status;
  648.     register volatile CtrlRegs *regsPtr; /* Host Adaptor registers */
  649.     register volatile unsigned char *initCmdPtr; /* pointer to initCmd reg */
  650.     register volatile unsigned char *modePtr; /* pointer to mode register */
  651.     register char *charPtr;
  652.     Controller    *ctrlPtr;
  653.     int i;
  654.     int size;                /* Number of bytes to transfer */
  655.     Address addr;            /* Kernel address of transfer */
  656.  
  657.     /*
  658.      * Set current active device and command for this controller.
  659.      */
  660.     ctrlPtr = devPtr->ctrlPtr;
  661.     SET_CTRL_BUSY(ctrlPtr,scsiCmdPtr);
  662.     ctrlPtr->dmaBuffer = (Address) NIL;
  663.     ctrlPtr->devPtr = devPtr;
  664.     size = scsiCmdPtr->bufferLen;
  665.     addr = scsiCmdPtr->buffer;
  666.     /*
  667.      * Determine the DMA state the size and direction of data transfer.
  668.      */
  669.     if (size == 0) {
  670.     ctrlPtr->dmaState = DMA_INACTIVE;
  671.     } else {
  672.     ctrlPtr->dmaState = (scsiCmdPtr->dataToDevice) ? DMA_SEND :
  673.                              DMA_RECEIVE;
  674.     }
  675.     ctrlPtr->dmaSetup = FALSE;
  676.     regsPtr = (volatile CtrlRegs *)ctrlPtr->regsPtr;
  677.     initCmdPtr = ®sPtr->sbc.write.initCmd;
  678.     modePtr = ®sPtr->sbc.write.mode;
  679.     /*
  680.      * Clear all control lines.
  681.      */
  682. #ifndef SCSI3_ONBOARD
  683.     /*
  684.      * For VME interface dis-allow DMA interrupts from reconnect attempts.
  685.      */
  686.     regsPtr->control &= ~SI_CSR_DMA_EN;
  687.     regsPtr->control |= SI_CSR_BPCON;    /* word byte packing */
  688. #endif
  689.     regsPtr->sbc.write.select = 0;
  690.     regsPtr->sbc.write.trgtCmd = 0;
  691.     regsPtr->sbc.write.initCmd = 0;
  692.     *modePtr &= ~SBC_MR_DMA;
  693.  
  694.     /*
  695.      * SCSI ARBITRATION.
  696.      *
  697.      * Arbitrate for the SCSI bus by putting our SCSI ID on the data
  698.      * bus and asserting the BUSY signal.  After an arbitration delay
  699.      * we look for other, higher priority IDs on the bus. (We won't find
  700.      * any because the Host Adaptor is wired in to be the highest.)
  701.      * Arbitration is completed by asserting the SELECT line.
  702.      */
  703.  
  704.     regsPtr->sbc.write.data = SI_HOST_ID;
  705.     for (i = 0; i < SBC_NUM_RETRIES; i++) {
  706.     /*
  707.      * Wait for the bus to go to BUS FREE - busy line not held.
  708.      */
  709.     for (i=0 ; i < WAIT_LENGTH ; i++) {
  710.         if ((regsPtr->sbc.read.curStatus & SBC_CBSR_BSY) == 0) {
  711.         break;
  712.         } else {
  713.         MACH_DELAY(10);
  714.         }
  715.     }
  716.     if (i == WAIT_LENGTH) {
  717.         /*
  718.          * Probably a higher level synchronization error.  The
  719.          * SCSI bus is probably busy with another transaction.
  720.          */
  721.         Reset(ctrlPtr);
  722.         return(FAILURE);
  723.     }
  724.     /*
  725.      * Enter Arbitration mode on the chip.
  726.      */
  727.     *modePtr |= SBC_MR_ARB;
  728.     status = WaitReg(ctrlPtr, (Address) ®sPtr->sbc.read.initCmd,
  729.                    REG_BYTE, SBC_ICR_AIP, NO_RESET, ACTIVE_HIGH);
  730.     if (status == DEV_TIMEOUT) {
  731.         continue;
  732.     }
  733.     if (status != SUCCESS) {
  734.         regsPtr->sbc.write.data = 0;
  735.         *modePtr &= ~SBC_MR_ARB;
  736.         return(status);
  737.     }
  738.     MACH_DELAY(SI_ARBITRATION_DELAY);
  739.     if (((regsPtr->sbc.read.initCmd & SBC_ICR_LA) == 0) &&
  740.         ((regsPtr->sbc.read.data & ~SI_HOST_ID)  < SI_HOST_ID)) {
  741.         break;
  742.     }
  743.     /*
  744.      * Lost arbitration due to reselection attempt by a target.
  745.      */
  746.     *modePtr &= ~SBC_MR_ARB;
  747.     /*
  748.      * A target may have tried to select us during arbitration phase.
  749.      * At this point we should save the current command and
  750.      * respond to the reconnection interrupt.
  751.      */
  752.     if ((regsPtr->sbc.read.curStatus & SBC_CBSR_SEL) && 
  753.         (regsPtr->sbc.read.curStatus & SBC_CBSR_IO) &&
  754.         (regsPtr->sbc.read.data & SI_HOST_ID)) {
  755.         return(FAILURE);
  756.     }
  757.     }
  758.     if (i == SBC_NUM_RETRIES) {
  759.     Reset(ctrlPtr);
  760.     return(FAILURE);    
  761.     }
  762.  
  763.     /*
  764.      * Arbitration complete.  Confirm by setting SELECT and BUSY.
  765.      * The ATN (attention) line would be set here if we want allow
  766.      * disconnection by the target.
  767.      */
  768.     *initCmdPtr = SBC_ICR_SEL | SBC_ICR_BUSY;
  769.     *modePtr &= ~SBC_MR_ARB;
  770.     MACH_DELAY(SI_BUS_CLEAR_DELAY + SI_BUS_SETTLE_DELAY);
  771.     /*
  772.      * SCSI SELECTION.
  773.      *
  774.      * Select the target by putting its ID plus our own on the bus
  775.      * and waiting for the target to assert the BUSY signal.  We
  776.      * drop SEL and DATA after the target responds.
  777.      */
  778.     regsPtr->sbc.write.data = (1 << devPtr->targetID) | SI_HOST_ID;
  779.     *initCmdPtr = SBC_ICR_SEL | SBC_ICR_DATA | SBC_ICR_BUSY;
  780.     MACH_DELAY(1);
  781.     *initCmdPtr  &= ~SBC_ICR_BUSY;
  782.     status = WaitReg(ctrlPtr, (Address) ®sPtr->sbc.read.curStatus,
  783.                REG_BYTE, SBC_CBSR_BSY, RESET, ACTIVE_HIGH);
  784.     if (status != SUCCESS) {
  785.     regsPtr->sbc.write.data = 0;
  786.     return(status);
  787.     }
  788.     *initCmdPtr &= ~(SBC_ICR_SEL | SBC_ICR_DATA);
  789.     /*
  790.      * Clear selection and DMA interrupts.
  791.      */
  792.     regsPtr->sbc.write.select = 0;
  793.     *modePtr &= ~SBC_MR_DMA;
  794.  
  795. #ifdef reselection
  796.     /*
  797.      * After target selection there is an optional message phase where
  798.      * we send an IDENTIFY message to indicate dis-connect capability.
  799.      */
  800.     data = SCSI_IDENDIFY | devPtr->handle.LUN;
  801.     status = PutByte(ctrlPtr, &data);
  802.     if (status != SUCCESS) {
  803.     return(status);
  804.     }
  805. #endif reselection
  806.     if (ctrlPtr->dmaState != DMA_INACTIVE) {
  807.     ctrlPtr->dmaBuffer = addr = VmMach_DMAAlloc(size,scsiCmdPtr->buffer);
  808.     /*
  809.      * DMA SETUP.
  810.      *
  811.      * First reset the DMA controllers so they
  812.      * don't complain with a DMA_CONFLICT interrupt.
  813.      */
  814. #ifdef SCSI3_ONBOARD
  815.         MACH_DELAY(SI_UDC_WAIT);
  816.         regsPtr->udcRaddr = UDC_ADR_COMMAND;
  817.         MACH_DELAY(SI_UDC_WAIT);
  818.         regsPtr->udcRdata = UDC_CMD_RESET;
  819.         MACH_DELAY(SI_UDC_WAIT);
  820. #endif
  821.     regsPtr->control &= ~SI_CSR_FIFO_RES;    
  822.     regsPtr->control |= SI_CSR_FIFO_RES;    
  823.     if (ctrlPtr->dmaState == DMA_RECEIVE) {
  824.         regsPtr->control &= ~SI_CSR_SEND;
  825.     } else {
  826.         regsPtr->control |= SI_CSR_SEND;
  827.     }
  828. #ifdef SCSI3_ONBOARD
  829.         {
  830.         register UDCDMAtable *udct = ctrlPtr->udcDmaTable;
  831.         /*
  832.          * Set fifoCount which is also wired to dmaCount, thus
  833.          * both registers are set.  The onboard DMA controller requires
  834.          * that these counts be set before entering the DATA PHASE.
  835.          */
  836.          regsPtr->fifoCountLow = size;
  837.         /*
  838.          * The onboard DMA controller expects a control block (!)
  839.          * that describes the DMA transfer.
  840.          */
  841.         udct->haddr = (((unsigned) addr & 0xff0000) >> 8) | UDC_ADDR_INFO;
  842.         udct->laddr = (unsigned)addr & 0xffff;
  843.         udct->hcmr = UDC_CMR_HIGH;
  844.         udct->count = size / 2; /* #bytes -> #words */
  845.  
  846.         if (ctrlPtr->dmaState == DMA_RECEIVE) {
  847.             udct->rsel = UDC_RSEL_RECV;
  848.             udct->lcmr = UDC_CMR_LRECV;
  849.         } else {
  850.             udct->rsel = UDC_RSEL_SEND;
  851.             udct->lcmr = UDC_CMR_LSEND;
  852.             if (size & 1) {
  853.                 udct->count++;
  854.             }
  855.         }
  856.         /*
  857.          * Now we tell the DMA chip where the control block is
  858.          * by setting the Chain Address Register (CAR).
  859.          */
  860.         regsPtr->udcRaddr = UDC_ADR_CAR_HIGH;
  861.         MACH_DELAY(SI_UDC_WAIT);
  862.         regsPtr->udcRdata = ((int)udct & 0xff0000) >> 8;
  863.         MACH_DELAY(SI_UDC_WAIT);
  864.         regsPtr->udcRaddr = UDC_ADR_CAR_LOW;
  865.         MACH_DELAY(SI_UDC_WAIT);
  866.         regsPtr->udcRdata = (int)udct & 0xffff;
  867.         /*
  868.          * Tell the chip to be a DMA master.
  869.          */
  870.         MACH_DELAY(SI_UDC_WAIT);
  871.         regsPtr->udcRaddr = UDC_ADR_MODE;
  872.         MACH_DELAY(SI_UDC_WAIT);
  873.         regsPtr->udcRdata = UDC_MODE;
  874.         /*
  875.          * Tell the chip to interrupt on error.
  876.          */
  877.         MACH_DELAY(SI_UDC_WAIT);
  878.         regsPtr->udcRaddr = UDC_ADR_COMMAND;
  879.         MACH_DELAY(SI_UDC_WAIT);
  880.         regsPtr->udcRdata = UDC_CMD_CIE;
  881.        }
  882. #else
  883.         /*
  884.          * Clear thing now, and set size later in StartDMA.
  885.          */
  886.         SET_FIFO_COUNT(regsPtr,0); 
  887.         SET_DMA_COUNT(regsPtr,0);
  888.         if (ctrlPtr->dmaState != DMA_INACTIVE) {
  889.         SET_DMA_ADDR(regsPtr,(unsigned)(addr - VMMACH_DMA_START_ADDR));
  890.         } else {
  891.         SET_DMA_ADDR(regsPtr,0);
  892.         }
  893. #endif
  894.     } else {
  895.     /*
  896.      * fifoCount register is wired to dmaCount so both are set.
  897.      */
  898.     SET_FIFO_COUNT(regsPtr,0);
  899.     }
  900.     regsPtr->control |= SI_CSR_INTR_EN;
  901.  
  902.     status = WaitPhase(ctrlPtr, PHASE_COMMAND, RESET);
  903.     if (status != SUCCESS) {
  904.     /*
  905.      * After we implement reselection it is at this point that
  906.      * we have to handle messages from targets.
  907.      */
  908.     return(status);
  909.     }
  910.     /*
  911.      * Stuff the control block through the commandStatus register.
  912.      * The handshake on the SCSI bus is visible here:  we have to
  913.      * wait for the Request line on the SCSI bus to be raised before
  914.      * we can send the next command byte to the controller.  Then we
  915.      * have to set the ACK line after putting out the data, and finnaly
  916.      * wait for the REQ line to drop again.
  917.      */
  918.     regsPtr->sbc.write.trgtCmd = TCR_COMMAND;
  919.     charPtr = scsiCmdPtr->commandBlock;
  920.     for (i=0 ; i< scsiCmdPtr->commandBlockLen; i++) {
  921.     /*
  922.      * SCSI DATA TRANSFER HANDSHAKE.
  923.      *
  924.      * Wait for Target to request data byte.
  925.      */
  926.     status = WaitReg(ctrlPtr, (Address)®sPtr->sbc.read.curStatus,
  927.                    REG_BYTE, SBC_CBSR_REQ, RESET, ACTIVE_HIGH);
  928.     if (status != SUCCESS) {
  929.         return(status);
  930.     }
  931.     /*
  932.      * Gate data onto SCSI bus and then set ACK.
  933.      */
  934.     regsPtr->sbc.write.data = *charPtr;
  935.     regsPtr->sbc.write.initCmd = SBC_ICR_DATA;
  936.     regsPtr->sbc.write.initCmd |= SBC_ICR_ACK;
  937.     /*
  938.      * Wait for Target to take byte and drop REQ.
  939.      */
  940.     status = WaitReg(ctrlPtr, (Address)®sPtr->sbc.read.curStatus,
  941.                    REG_BYTE, SBC_CBSR_REQ, RESET, ACTIVE_LOW);
  942.     if (status != SUCCESS) {
  943.         return(status);
  944.     }
  945.     charPtr++;
  946.     if (i < scsiCmdPtr->commandBlockLen - 1) {
  947.         /*
  948.          * Finally we drop the ACK line.
  949.          */
  950.         regsPtr->sbc.write.initCmd = 0;
  951.     }
  952.     }
  953.  
  954.     i = regsPtr->sbc.read.clear;
  955.     regsPtr->sbc.write.select = SI_HOST_ID;
  956.     regsPtr->sbc.write.trgtCmd = TCR_UNSPECIFIED;
  957.     *modePtr |= SBC_MR_DMA;
  958.     regsPtr->sbc.write.initCmd = 0;
  959. #ifndef SCSI3_ONBOARD
  960.     regsPtr->control |= SI_CSR_DMA_EN;
  961. #endif
  962.     status = SUCCESS;
  963.     return(status);
  964. }
  965.  
  966.  
  967. /*
  968.  *----------------------------------------------------------------------
  969.  *
  970.  * StartDMA --
  971.  *
  972.  *    Issue the sequence of commands to the controller to start DMA.
  973.  *    This can be called by Dev_SCSI3Intr in response to a DATA_{IN,OUT}
  974.  *    phase message.
  975.  *
  976.  * Results:
  977.  *    None.
  978.  *
  979.  * Side effects:
  980.  *    DMA is enabled.  No registers other than the control register are
  981.  *    to be accessed until DMA is disabled again.
  982.  *
  983.  *----------------------------------------------------------------------
  984.  */
  985. static void
  986. StartDMA(ctrlPtr)
  987.     register Controller *ctrlPtr;
  988. {
  989.     register volatile CtrlRegs *regsPtr;
  990.     unsigned char junk;
  991.     int    size;
  992.  
  993.     size = ctrlPtr->scsiCmdPtr->bufferLen;
  994.  
  995.     regsPtr = ctrlPtr->regsPtr;
  996. #ifdef SCSI3_ONBOARD
  997.     /*
  998.      * The DMA control block has already been set up.  We just say "go".
  999.      */
  1000.     MACH_DELAY(SI_UDC_WAIT);
  1001.     regsPtr->udcRdata = UDC_CMD_STRT_CHN;
  1002. #else
  1003.         SET_DMA_COUNT(regsPtr,size);
  1004.     ctrlPtr->dmaSetup = TRUE;
  1005. #endif
  1006.  
  1007.     if (ctrlPtr->dmaState == DMA_RECEIVE) {
  1008.     regsPtr->sbc.write.trgtCmd = TCR_DATA_IN;
  1009.     junk = regsPtr->sbc.read.clear;
  1010.     regsPtr->sbc.write.mode |= SBC_MR_DMA;
  1011.     regsPtr->sbc.write.initRecv = 0;
  1012.     } else {
  1013.     regsPtr->sbc.write.trgtCmd = TCR_DATA_OUT;
  1014.     junk = regsPtr->sbc.read.clear;
  1015. #ifdef lint
  1016.     regsPtr->sbc.read.clear = junk;
  1017. #endif
  1018.     regsPtr->sbc.write.initCmd = SBC_ICR_DATA;
  1019.     regsPtr->sbc.write.mode |= SBC_MR_DMA;
  1020.     regsPtr->sbc.write.send = 0;
  1021.     }
  1022. #ifndef SCSI3_ONBOARD
  1023.     regsPtr->control |= SI_CSR_DMA_EN;
  1024. #endif
  1025. }
  1026.  
  1027.  
  1028. /*
  1029.  *----------------------------------------------------------------------
  1030.  *
  1031.  * GetStatusByte --
  1032.  *
  1033.  *    Complete an SCSI command by getting the status bytes from
  1034.  *    the device and waiting for the ``command complete''
  1035.  *    message that follows the status bytes.  
  1036.  *
  1037.  * Results:
  1038.  *    An error code if the status didn't come through or it
  1039.  *    indicated an error.
  1040.  *
  1041.  * Side effects:
  1042.  *    None.
  1043.  *
  1044.  *----------------------------------------------------------------------
  1045.  */
  1046. static ReturnStatus
  1047. GetStatusByte(ctrlPtr,statusBytePtr)
  1048.     Controller *ctrlPtr;        /* Controller to get byte from. */
  1049.     unsigned char *statusBytePtr;    /* Where to put the status byte. */
  1050. {
  1051.     register volatile CtrlRegs *regsPtr;
  1052.     ReturnStatus status;
  1053.     char message;
  1054.  
  1055.     regsPtr = (volatile CtrlRegs *)ctrlPtr->regsPtr;
  1056.     *statusBytePtr = 0;
  1057.  
  1058.     /*
  1059.      * After the DATA_IN/OUT phase we enter the STATUS phase for
  1060.      * 1 byte (usually) of status.  This is followed by the MESSAGE phase
  1061.      */
  1062.     status = WaitPhase(ctrlPtr, PHASE_STATUS, RESET);
  1063.     if (status != SUCCESS) {
  1064.     return(status);
  1065.     }
  1066.     /*
  1067.      * Get one status byte.
  1068.      */
  1069.     status = GetByte(ctrlPtr, PHASE_STATUS, (char *) statusBytePtr);
  1070.     if (status != SUCCESS) {
  1071.     return (status);
  1072.     }
  1073. #ifdef notdef
  1074.     /* 
  1075.      * From the way the code was originally written it looks like some
  1076.      * devices return more that one byte of status info. Since we don't
  1077.      * want these bytes drop them on the floor.
  1078.      */
  1079.     for (; ; ) {
  1080.         status = GetByte(ctrlPtr, PHASE_STATUS, (char *) statusBytePtr);
  1081.     }
  1082.         *statusBytePtr = statusByte;
  1083.         statusBytePtr++;
  1084.     }
  1085.     numStatusBytes++;
  1086.     }
  1087. #endif
  1088.     /*
  1089.      * Wait for the message in phase and grap the COMMAND COMPLETE message
  1090.      * off the bus.
  1091.      */
  1092.     status = WaitPhase(ctrlPtr, PHASE_MSG_IN, RESET);
  1093.     if (status != SUCCESS) {
  1094.     return(status);
  1095.     } 
  1096.     status = GetByte(ctrlPtr, PHASE_MSG_IN, &message);
  1097.     if (status != SUCCESS) {
  1098.     return(status);
  1099.     }
  1100.     if (message != SCSI_COMMAND_COMPLETE) {
  1101.     return(FAILURE);
  1102.     }
  1103.     regsPtr->sbc.write.trgtCmd = TCR_UNSPECIFIED;
  1104.     return(SUCCESS);
  1105. }
  1106.  
  1107.  
  1108. /*
  1109.  *----------------------------------------------------------------------
  1110.  *
  1111.  * WaitReg --
  1112.  *
  1113.  *    Wait for any of a set of bits to be enabled 
  1114.  *    in the specified register.  The generic regsPtr pointer is
  1115.  *    passed in so this routine can check for bus and parity errors.
  1116.  *    A pointer to the register to check, and an indicator of its type
  1117.  *    (its size) are passed in as well.  Finally, the conditions
  1118.  *     can be awaited to become 1 or 0.
  1119.  *
  1120.  * Results:
  1121.  *    SUCCESS if the condition occurred before a threshold time limit,
  1122.  *    DEV_TIMEOUT otherwise.
  1123.  *
  1124.  * Side effects:
  1125.  *    This resets the SCSI bus if the reset parameter is true and
  1126.  *    the condition bits are not set by the controller before timeout.
  1127.  *
  1128.  *----------------------------------------------------------------------
  1129.  */
  1130. static ReturnStatus
  1131. WaitReg(ctrlPtr, thisRegPtr, type, conditions, reset, bitSel)
  1132.     Controller *ctrlPtr;    /* Controller state */
  1133.     Address thisRegPtr;        /* pointer to register to check */
  1134.     RegType type;        /* "type" of the register */
  1135.     unsigned int conditions;    /* one or more bits to check */
  1136.     Boolean reset;        /* whether to reset the bus on error */
  1137.     BitSelection bitSel;    /* check for all or some bits going to 1/0 */
  1138. {
  1139.     volatile register CtrlRegs *regsPtr = (volatile CtrlRegs *)ctrlPtr->regsPtr;
  1140.     register int i;
  1141.     ReturnStatus status = DEV_TIMEOUT;
  1142.     register unsigned int thisReg;
  1143.  
  1144.     for (i=0 ; i<WAIT_LENGTH ; i++) {
  1145.     switch (type) {
  1146.         case REG_BYTE: {
  1147.         unsigned char *charPtr = (unsigned char *) thisRegPtr;
  1148.         thisReg = (unsigned int) *charPtr;
  1149.         break;
  1150.         }
  1151.         case REG_SHORT: {
  1152.         unsigned short *shortPtr = (unsigned short *) thisRegPtr;
  1153.         thisReg = (unsigned int) *shortPtr;
  1154.         break;
  1155.         }
  1156.         default: {
  1157.         break;
  1158.         }
  1159.     }
  1160.         
  1161.     switch(bitSel) {
  1162.         case ACTIVE_HIGH: {
  1163.         if ((thisReg & conditions) != 0) {
  1164.             return(SUCCESS);
  1165.         }
  1166.         break;
  1167.         }
  1168.         case ACTIVE_ALL: {
  1169.         if ((thisReg & conditions) == conditions) {
  1170.             return(SUCCESS);
  1171.         }
  1172.         break;
  1173.         }
  1174.         case ACTIVE_LOW: {
  1175.         if ((thisReg & conditions) != conditions) {
  1176.             return(SUCCESS);
  1177.         }
  1178.         break;
  1179.         }
  1180.         case ACTIVE_NONE: {
  1181.         if ((thisReg & conditions) == 0) {
  1182.             return(SUCCESS);
  1183.         }
  1184.         break;
  1185.         }
  1186.         default: {
  1187.         break;
  1188.         }
  1189.     } 
  1190.     if (regsPtr->control & SI_CSR_DMA_BUS_ERR) {
  1191.         status = DEV_DMA_FAULT;
  1192.         break;
  1193. #ifdef notdef
  1194.     } else if (regsPtr->sbc.read.status & SBC_BSR_PERR) {
  1195.         status = DEV_DMA_FAULT;
  1196.         break;
  1197. #endif
  1198.     }
  1199.     MACH_DELAY(10);
  1200.     }
  1201.      if (reset) {
  1202.     Reset(ctrlPtr);
  1203.     }
  1204.     return(status);
  1205. }
  1206.  
  1207. /*
  1208.  *----------------------------------------------------------------------
  1209.  *
  1210.  * WaitPhase --
  1211.  *
  1212.  *    Wait for a phase to be signalled in the controller registers.
  1213.  *     This is a specialized version of WaitReg, which compares
  1214.  *     all the phase bits to make sure the phase is exactly what is
  1215.  *    requested and not something that matches only in some bits.
  1216.  *
  1217.  * Results:
  1218.  *    SUCCESS if the condition occurred before a threshold time limit,
  1219.  *    DEV_TIMEOUT otherwise.
  1220.  *
  1221.  * Side effects:
  1222.  *    This resets the SCSI bus if the condition bits are not set by
  1223.  *    the controller before timeout.
  1224.  *
  1225.  *----------------------------------------------------------------------
  1226.  */
  1227. static ReturnStatus
  1228. WaitPhase(ctrlPtr, phase, reset)
  1229.     Controller *ctrlPtr;    /* Controller state */
  1230.     unsigned char phase;    /* phase to check */
  1231.     Boolean reset;        /* whether to reset the bus on error */
  1232. {
  1233.     volatile register CtrlRegs *regsPtr = (volatile CtrlRegs *)ctrlPtr->regsPtr;
  1234.     register int i;
  1235.     ReturnStatus status = DEV_TIMEOUT;
  1236.     register unsigned char thisReg;
  1237.  
  1238.     for (i=0 ; i<WAIT_LENGTH ; i++) {
  1239.     thisReg = regsPtr->sbc.read.curStatus;
  1240.     if ((thisReg & CBSR_PHASE_BITS) == phase) {
  1241.         return(SUCCESS);
  1242.     }
  1243.     if (regsPtr->control & SI_CSR_DMA_BUS_ERR) {
  1244.         status = DEV_DMA_FAULT;
  1245.         break;
  1246. #ifdef notdef
  1247.     } else if (regsPtr->sbc.read.status & SBC_BSR_PERR) {
  1248.         if (devSCSI3Debug > 0) {
  1249.             panic("SCSI3: parity error\n");
  1250.         } else {
  1251.         printf("SCSI3: parity error\n");
  1252.         }
  1253.         status = DEV_DMA_FAULT;
  1254.         break;
  1255. #endif
  1256.     }
  1257.     MACH_DELAY(10);
  1258.     }
  1259.     if (reset) {
  1260.     Reset(ctrlPtr);
  1261.     }
  1262.     return(status);
  1263. }
  1264.  
  1265. /*
  1266.  *----------------------------------------------------------------------
  1267.  *
  1268.  * PutByte --
  1269.  *
  1270.  *    Put a byte onto the SCSI bus.  This always goes into the MSG_OUT
  1271.  *    phase.  This handles the standard REQ/ACK handshake to put
  1272.  *    the bytes on the SCSI bus.
  1273.  *
  1274.  * Results:
  1275.  *    None.
  1276.  *
  1277.  * Side effects:
  1278.  *    Yanks control lines in order to put a byte on the bus.
  1279.  *
  1280.  *----------------------------------------------------------------------
  1281.  */
  1282.  
  1283. static ReturnStatus
  1284. PutByte(ctrlPtr, dataPtr)
  1285.     Controller *ctrlPtr;
  1286.     char *dataPtr;    
  1287. {
  1288.     volatile register CtrlRegs *regsPtr = (volatile CtrlRegs *) ctrlPtr->regsPtr;
  1289.     volatile unsigned char *initCmdPtr = ®sPtr->sbc.write.initCmd;
  1290.     register ReturnStatus status;
  1291.     unsigned char junk;
  1292.  
  1293.     /*
  1294.      * Enter MESSAGE OUT phase and wait for REQ to be set by the target.
  1295.      */
  1296.     regsPtr->sbc.write.trgtCmd = TCR_MSG_OUT;
  1297.     *initCmdPtr = 0;
  1298.     status = WaitReg(ctrlPtr, (Address) ®sPtr->sbc.read.curStatus,
  1299.                 REG_BYTE, SBC_CBSR_REQ, RESET, ACTIVE_HIGH);
  1300.     if (status != SUCCESS) {
  1301.     return(status);
  1302.     }
  1303.     /*
  1304.      * Put the data on and then ACK the target's REQ.
  1305.      */
  1306.     regsPtr->sbc.write.data = *dataPtr;
  1307.     regsPtr->sbc.write.initCmd = SBC_ICR_DATA;
  1308.     regsPtr->sbc.write.initCmd |= SBC_ICR_ACK;
  1309.     status =  WaitReg(ctrlPtr, (Address) ®sPtr->sbc.read.curStatus,
  1310.                 REG_BYTE, SBC_CBSR_REQ, RESET, ACTIVE_LOW);
  1311.     if (status != SUCCESS) {
  1312.     return(status);
  1313.     }    
  1314.  
  1315.     regsPtr->sbc.write.trgtCmd = TCR_UNSPECIFIED;
  1316.     junk = regsPtr->sbc.read.clear;
  1317. #ifdef lint
  1318.     regsPtr->sbc.read.clear = junk;
  1319. #endif
  1320.     regsPtr->sbc.write.initCmd = 0;
  1321.     return(SUCCESS);
  1322. }
  1323.  
  1324.  
  1325. /*
  1326.  *----------------------------------------------------------------------
  1327.  *
  1328.  * GetByte --
  1329.  *
  1330.  *    Get a byte from the SCSI bus, corresponding to the specified phase.
  1331.  *     This entails waiting for a request, checking the phase, reading
  1332.  *    the byte, and sending an acknowledgement.
  1333.  *
  1334.  * Results:
  1335.  *    None.
  1336.  *
  1337.  * Side effects:
  1338.  *    None.
  1339.  *
  1340.  *----------------------------------------------------------------------
  1341.  */
  1342.  
  1343. static ReturnStatus
  1344. GetByte(ctrlPtr, phase, charPtr)
  1345.     Controller *ctrlPtr;
  1346.     unsigned short phase;
  1347.     char *charPtr;    
  1348. {
  1349.     register volatile CtrlRegs *regsPtr = (volatile CtrlRegs *)ctrlPtr->regsPtr;
  1350.     ReturnStatus status;
  1351.     
  1352.     status = WaitReg(ctrlPtr, (Address) ®sPtr->sbc.read.curStatus,
  1353.                 REG_BYTE, SBC_CBSR_REQ, RESET, ACTIVE_HIGH);
  1354.     if (status != SUCCESS) {
  1355.     return(status);
  1356.     }
  1357.     if ((regsPtr->sbc.read.curStatus & CBSR_PHASE_BITS) != phase) {
  1358.     /*
  1359.      * Use the "handshake error" to signal a new phase.  This should
  1360.      * be propagated into a new DEV status to signal a "condition" that
  1361.      * isn't an "error".
  1362.      */
  1363.     return(DEV_HANDSHAKE_ERROR);
  1364.     }
  1365.     *charPtr = regsPtr->sbc.read.data;
  1366.     regsPtr->sbc.write.initCmd = SBC_ICR_ACK;
  1367.     status = WaitReg(ctrlPtr, (Address) ®sPtr->sbc.read.curStatus,
  1368.                 REG_BYTE, SBC_CBSR_REQ, RESET, ACTIVE_LOW);
  1369.     if (status != SUCCESS) {
  1370.     return(status);
  1371.     }
  1372.     if ((phase == PHASE_MSG_IN) && (*charPtr == SCSI_COMMAND_COMPLETE)) {
  1373.     regsPtr->sbc.write.initCmd = 0;
  1374.     regsPtr->sbc.write.mode &= ~SBC_MR_DMA;
  1375.     } else {
  1376.         regsPtr->sbc.write.initCmd = 0;
  1377.     }
  1378.     return(SUCCESS);
  1379. }
  1380.  
  1381.  
  1382. /*
  1383.  *----------------------------------------------------------------------
  1384.  *
  1385.  *  SpecialSenseProc --
  1386.  *
  1387.  *    Special function used for HBA generated REQUEST SENSE. A SCSI
  1388.  *    command request with this function as a call back proc will
  1389.  *    be processed by routine RequestDone as a result of a 
  1390.  *    REQUEST SENSE. This routine is never called.
  1391.  *
  1392.  * Results:
  1393.  *    None.
  1394.  *
  1395.  * Side effects:
  1396.  *    None.
  1397.  *
  1398.  *----------------------------------------------------------------------
  1399.  */
  1400.  
  1401. static int
  1402. SpecialSenseProc()
  1403. {
  1404. }
  1405.  
  1406.  
  1407. /*
  1408.  *----------------------------------------------------------------------
  1409.  *
  1410.  * RequestDone --
  1411.  *
  1412.  *    Process a request that has finished. Unless a SCSI check condition
  1413.  *    bit is present in the status returned, the request call back
  1414.  *    function is called.  If check condition is set we fire off a
  1415.  *    SCSI REQUEST SENSE to get the error sense bytes from the device.
  1416.  *
  1417.  * Results:
  1418.  *    None.
  1419.  *
  1420.  * Side effects:
  1421.  *    The call back function may be called.
  1422.  *
  1423.  *----------------------------------------------------------------------
  1424.  */
  1425.  
  1426. static void
  1427. RequestDone(devPtr,scsiCmdPtr,status,scsiStatusByte,amountTransferred)
  1428.     Device    *devPtr;    /* Device for request. */
  1429.     ScsiCmd    *scsiCmdPtr;    /* Request that finished. */
  1430.     ReturnStatus status;    /* Status returned. */
  1431.     unsigned char scsiStatusByte;    /* SCSI Status Byte. */
  1432.     int        amountTransferred; /* Amount transferred by command. */
  1433. {
  1434.     ReturnStatus    senseStatus;
  1435.     Controller            *ctrlPtr = devPtr->ctrlPtr;
  1436.  
  1437.  
  1438.      /*
  1439.      * Unallocated any DMA allocated for this request. 
  1440.      */
  1441.     if (ctrlPtr->dmaBuffer != (Address) NIL) {
  1442.     VmMach_DMAFree(scsiCmdPtr->bufferLen, ctrlPtr->dmaBuffer);
  1443.     ctrlPtr->dmaBuffer = (Address) NIL;
  1444.     }
  1445.     /*
  1446.      * First check to see if this is the reponse of a HBA generated 
  1447.      * REQUEST SENSE command.  If this is the case, we can process
  1448.      * the callback of the frozen command for this device and
  1449.      * allow the flow of command to the device to be resummed.
  1450.      */
  1451.     if (scsiCmdPtr->doneProc == SpecialSenseProc) {
  1452.         MASTER_UNLOCK(&(ctrlPtr->mutex));
  1453.     (devPtr->frozen.scsiCmdPtr->doneProc)(devPtr->frozen.scsiCmdPtr, 
  1454.             SUCCESS,
  1455.             devPtr->frozen.statusByte, 
  1456.             devPtr->frozen.amountTransferred,
  1457.             amountTransferred,
  1458.             devPtr->senseBuffer);
  1459.          MASTER_LOCK(&(ctrlPtr->mutex));
  1460.      SET_CTRL_FREE(ctrlPtr);
  1461.      return;
  1462.     }
  1463.     /*
  1464.      * This must be a outside request finishing. If the request 
  1465.      * suffered an error or the HBA or the scsi status byte
  1466.      * says there is no error sense present, we can do the
  1467.      * callback and free the controller.
  1468.      */
  1469.     if ((status != SUCCESS) || !SCSI_CHECK_STATUS(scsiStatusByte)) {
  1470.         MASTER_UNLOCK(&(ctrlPtr->mutex));
  1471.     (scsiCmdPtr->doneProc)(scsiCmdPtr, status, scsiStatusByte,
  1472.                    amountTransferred, 0, (char *) 0);
  1473.         MASTER_LOCK(&(ctrlPtr->mutex));
  1474.      SET_CTRL_FREE(ctrlPtr);
  1475.      return;
  1476.    } 
  1477.    /*
  1478.     * If we got here than the SCSI command came back from the device
  1479.     * with the CHECK bit set in the status byte.
  1480.     * Need to perform a REQUEST SENSE. Move the current request 
  1481.     * into the frozen state and issue a REQUEST SENSE. 
  1482.     */
  1483.    devPtr->frozen.scsiCmdPtr = scsiCmdPtr;
  1484.    devPtr->frozen.statusByte = scsiStatusByte;
  1485.    devPtr->frozen.amountTransferred = amountTransferred;
  1486.    DevScsiSenseCmd((ScsiDevice *)devPtr, DEV_MAX_SENSE_BYTES, 
  1487.            devPtr->senseBuffer, &(devPtr->SenseCmd));
  1488.    devPtr->SenseCmd.doneProc = SpecialSenseProc,
  1489.    senseStatus = SendCommand(devPtr, &(devPtr->SenseCmd));
  1490.    /*
  1491.     * If we got an HBA error on the REQUEST SENSE we end the outside 
  1492.     * command with the SUCCESS status but zero sense bytes returned.
  1493.     */
  1494.    if (senseStatus != SUCCESS) {
  1495.         MASTER_UNLOCK(&(ctrlPtr->mutex));
  1496.     (scsiCmdPtr->doneProc)(scsiCmdPtr, status, scsiStatusByte,
  1497.                    amountTransferred, 0, (char *) 0);
  1498.         MASTER_LOCK(&(ctrlPtr->mutex));
  1499.     SET_CTRL_FREE(ctrlPtr);
  1500.    }
  1501.  
  1502. }
  1503.  
  1504. /*
  1505.  *----------------------------------------------------------------------
  1506.  *
  1507.  * entryAvailProc --
  1508.  *
  1509.  *    Act upon an entry becomming available in the queue for this
  1510.  *    controller. This routine is the Dev_Queue callback function that
  1511.  *    is called whenever work becomes available for this controller. 
  1512.  *    If the controller is not already busy we dequeue and start the
  1513.  *    request.
  1514.  *    NOTE: This routine is also called from DevSCSI3Intr to start the
  1515.  *    next request after the previously one finishes.
  1516.  *
  1517.  * Results:
  1518.  *    None.
  1519.  *
  1520.  * Side effects:
  1521.  *    Request may be dequeue and submitted to the device. Request callback
  1522.  *    function may be called.
  1523.  *
  1524.  *----------------------------------------------------------------------
  1525.  */
  1526.  
  1527. static Boolean
  1528. entryAvailProc(clientData, newRequestPtr) 
  1529.    ClientData    clientData;    /* Really the Device this request ready. */
  1530.    List_Links *newRequestPtr;    /* The new SCSI request. */
  1531. {
  1532.     register Device *devPtr; 
  1533.     register Controller *ctrlPtr;
  1534.     register ScsiCmd    *scsiCmdPtr;
  1535.     ReturnStatus    status;
  1536.  
  1537.     devPtr = (Device *) clientData;
  1538.     ctrlPtr = devPtr->ctrlPtr;
  1539.     scsiCmdPtr = (ScsiCmd *) newRequestPtr;
  1540.     devPtr = (Device *) clientData;
  1541.     status = SendCommand(devPtr, scsiCmdPtr);
  1542.     /*    
  1543.      * If the command couldn't be started do the callback function.
  1544.      */
  1545.     if (status != SUCCESS) {
  1546.      RequestDone(devPtr,scsiCmdPtr,status,0,0);
  1547.     }
  1548.     return TRUE;
  1549.  
  1550. }   
  1551.  
  1552.  
  1553.  
  1554. /*
  1555.  *----------------------------------------------------------------------
  1556.  *
  1557.  * DevSCSI3Intr --
  1558.  *
  1559.  * Handle interrupts from the SCSI-3 controller.
  1560.  * The follow cases cause interrupts on the 5380: => SBC_IP
  1561.  *    1. Reselection attempt by a target (not implemented yet)
  1562.  *    2. EOP during DMA.  This indicates DMA has completed.
  1563.  *    3. SCSI bus reset.  (Only applies to targets, not us.)
  1564.  *    4. Parity error during data transfer.  (Parity checking is disabled.)
  1565.  *    5. Bus phase mismatch.  trgtCmd must be correct for data transfers.
  1566.  *    6. SCSI bus disconnect by a target (not implmented yet)
  1567.  * In addition the SCSI-3 Host Adaptor will generate interrupts when:
  1568.  *    7. Registers other than control are touched during DMA => DMA_CONFLICT
  1569.  *    8. An error occurs during DMA. => DMA_IP
  1570.  *
  1571.  * Results:
  1572.  *    TRUE if an SCSI3 controller was responsible for the interrupt
  1573.  *    and this routine handled it.
  1574.  *
  1575.  * Side effects:
  1576.  *    Usually a process is notified that an I/O has completed.
  1577.  *
  1578.  *----------------------------------------------------------------------
  1579.  */
  1580. Boolean 
  1581. DevSCSI3Intr(clientDataArg)
  1582.     ClientData    clientDataArg;
  1583. {
  1584.     Controller *ctrlPtr;
  1585.     register volatile CtrlRegs *regsPtr;
  1586.     Device    *devPtr;
  1587.     unsigned char statusByte;
  1588.     ReturnStatus status;
  1589.     int byteCount;
  1590.     register int i;
  1591.     unsigned char phase;
  1592.     unsigned char foo;
  1593.     int     residual;
  1594.     List_Links    *newRequestPtr;
  1595.     ClientData    clientData;
  1596.  
  1597.     ctrlPtr = (Controller *) Controllers[0];
  1598.     regsPtr = ctrlPtr->regsPtr;
  1599.     devPtr = ctrlPtr->devPtr;
  1600.     if ((regsPtr->control & (SI_CSR_SBC_IP |    /* 5380 interrupt */
  1601.                  SI_CSR_DMA_IP |        /* or DMA error */
  1602.                  SI_CSR_DMA_CONFLICT))    /* or register goof */
  1603.                  == 0) {
  1604.     return FALSE;
  1605.     }
  1606.     MASTER_LOCK(&(ctrlPtr->mutex));
  1607.     if (ctrlPtr->scsiCmdPtr != (ScsiCmd *) NIL) {
  1608.     residual = ctrlPtr->scsiCmdPtr->bufferLen;
  1609.     }
  1610.    /*
  1611.      * First, disable DMA or else we'll get register conflicts.
  1612.      */
  1613. #ifndef SCSI3_ONBOARD
  1614.     regsPtr->control &= ~SI_CSR_DMA_EN;
  1615.     byteCount = ctrlPtr->dmaSetup ? READ_FIFO_COUNT(regsPtr) : residual;
  1616. #else
  1617.     byteCount = READ_FIFO_COUNT(regsPtr);
  1618. #endif
  1619.     regsPtr->sbc.write.trgtCmd = TCR_UNSPECIFIED;
  1620.     /*
  1621.      * The follow cases cause interrupts on the 5380:
  1622.      *    1. Selection or Reselection (only reselection applies to the CPU)
  1623.      *    2. EOP during DMA.  This indicates DMA has completed.
  1624.      *    3. SCSI bus reset.  This probably shouldn't happen; we do the resetting
  1625.      *    4. Parity error during data transfer.  Parity checking is disabled.
  1626.      *    5. Bus phase mismatch.  trgtCmd must be correct for data transfers.
  1627.      *    6. SCSI bus disconnect.  A target is disconnecting.
  1628.      * In addition the Host Adaptor will generate interrupts when:
  1629.      *    7. Registers other than control are touched during DMA => DMA_CONFLICT
  1630.      *    8. An error occurs during DMA. => DMA_IP
  1631.      */
  1632.     if (regsPtr->control & (SI_CSR_DMA_IP | SI_CSR_DMA_CONFLICT)) {
  1633.     /*
  1634.      * DMA Error.  DMA_IP means a bus error or
  1635.      *            "send & fifo-empty & dmaCount == 0"
  1636.      *    DMA_CONFLICT means we touched a non-control reg during DMA.
  1637.      */
  1638.     if (regsPtr->control & SI_CSR_DMA_BUS_ERR) {
  1639.         /*
  1640.          * A Bus Error.  Complete the I/O but flag an error.
  1641.          * The residual is computed because the Bus Error could
  1642.          * have occurred after a number of sectors.
  1643.          */
  1644.         residual = byteCount;
  1645.     } 
  1646.     status = DEV_DMA_FAULT;
  1647.     goto rtnHardErrorAndGetNext;    /* Return the hard error message
  1648.                      * to the call and get the next 
  1649.                      * entry in the devQueue. */
  1650.     }
  1651.     /*
  1652.      * 5380 generated interrupt.
  1653.      * Interrupt processing is described on pages 86-89.
  1654.      *    Parity is turned off, so SBC_BSR_PERR can be ignored.
  1655.      *    Busy monitoring mode is not set, so SBC_BSR_BERR can be ignored.
  1656.      */
  1657.     if (regsPtr->sbc.read.curStatus & SBC_CBSR_SEL) {
  1658.     /*
  1659.      * Reselection attempt by a target.  Unimplementned.
  1660.      */
  1661.     foo = regsPtr->sbc.read.clear;
  1662.     MASTER_UNLOCK(&(ctrlPtr->mutex));
  1663.     return(TRUE);
  1664.     }
  1665.     /*
  1666.      * SBC_BSR_EDMA may be set to indicate that DMA has completed,
  1667.      * or the SBC_BSR_PMTCH bit is 0 (this has been verified).
  1668.      * We fall through and test the REQ line to see if the target
  1669.      * is trying to send additional data bytes, or we are just
  1670.      * getting a standard phase change interrupt.
  1671.      */
  1672.     for (i=0 ; i<30 ; i++) {
  1673.     if (regsPtr->sbc.read.curStatus & SBC_CBSR_REQ) {
  1674.         break;
  1675.     }
  1676.     MACH_DELAY(10);
  1677.     }
  1678.     foo = regsPtr->sbc.read.clear;
  1679. #ifdef lint        
  1680.     regsPtr->sbc.read.clear = foo;
  1681. #endif
  1682.     if (i == 30) {
  1683.     /*
  1684.      * Apparently spurious interrupt, cause as yet unknown.
  1685.      */
  1686.     MASTER_UNLOCK(&(ctrlPtr->mutex));
  1687.     return(TRUE);
  1688.     }
  1689.     phase = regsPtr->sbc.read.curStatus & CBSR_PHASE_BITS;
  1690.     switch (phase) {
  1691.     case PHASE_DATA_IN:
  1692.     case PHASE_DATA_OUT: {
  1693.         regsPtr->sbc.write.mode &= ~SBC_MR_DMA;
  1694.         StartDMA(ctrlPtr);
  1695.         MASTER_UNLOCK(&(ctrlPtr->mutex));
  1696.         return(TRUE);
  1697.     }
  1698.     case PHASE_MSG_IN: {
  1699.         char    message;
  1700.         status = GetByte(ctrlPtr, PHASE_MSG_IN, (char *)&message);
  1701.         if (status != SUCCESS) {
  1702. #ifndef SCSI3_ONBOARD
  1703.             regsPtr->control |= SI_CSR_DMA_EN;
  1704. #endif
  1705.         MASTER_UNLOCK(&(ctrlPtr->mutex));
  1706.         return(TRUE);
  1707.         }
  1708.         if (message != SCSI_COMMAND_COMPLETE) {
  1709. #ifndef SCSI3_ONBOARD
  1710.             regsPtr->control |= SI_CSR_DMA_EN;
  1711. #endif
  1712.         }
  1713.         MASTER_UNLOCK(&(ctrlPtr->mutex));
  1714.         return(TRUE);
  1715.     }
  1716.     case PHASE_STATUS: {
  1717. #ifdef SCSI3_ONBOARD
  1718.         residual = byteCount;
  1719. #else
  1720.         residual = ctrlPtr->dmaSetup ? READ_DMA_COUNT(regsPtr) :
  1721.                         byteCount;
  1722. #endif
  1723.         if (ctrlPtr->dmaState == DMA_RECEIVE) {
  1724. #ifndef SCSI3_ONBOARD
  1725.             if ((regsPtr->control & SI_CSR_LOB) != 0) {
  1726.             /*
  1727.              * On a read the last odd byte is left in the byte pack
  1728.              * register. We use wordmode (not 32-bit longwords)
  1729.              * so there will only be one byte left.  I assume it
  1730.              * is Byte 0 in the byte pack reg, with is the first
  1731.              * byte in the high-half.
  1732.              */ 
  1733.             *(char *) (READ_DMA_ADDR(regsPtr) + VMMACH_DMA_START_ADDR) = 
  1734.                 (regsPtr->bytePackHigh & 0xff00) >> 8;
  1735.             }
  1736. #else
  1737.             regsPtr->udcRaddr = UDC_ADR_COUNT;
  1738.             /*
  1739.              * wait for the fifo to empty
  1740.              */
  1741.            status = WaitReg(ctrlPtr, (Address)®sPtr->control,
  1742.                 REG_SHORT, SI_CSR_FIFO_EMPTY, RESET, ACTIVE_HIGH);
  1743.            if (status != SUCCESS) {
  1744.            }
  1745. #ifdef notsure            
  1746.             if ((READ_FIFO_COUNT(regsPtr) == size)  ||
  1747.             (READ_FIFO_COUNT(regsPtr) + 1 == size) {
  1748.                 goto out;
  1749.             /*
  1750.              * Didn't transfer any data.
  1751.              * The fifoCount + 1 above wards against 5380 prefetch.
  1752.              */
  1753.                 goto out;
  1754.              }
  1755.             /*
  1756.              * Transfer left over byte or shortword by hand
  1757.              */
  1758.             if ((size - READ_FIFO_COUNT(regsPtr)) & 1) {
  1759.             *(char *)(READ_DMA_ADDR(regsPtr) - 1 + VMMACH_DMA_START_ADDR) =
  1760.                 (regsPtr->fifoData & 0xff00) >> 8;
  1761.             } else if (((regsPtr->udcRdata*2) - READ_FIFO_COUNT(regsPtr)) == 2) {
  1762.             *(char *)(READ_DMA_ADDR(regsPtr) - 2 + VMMACH_DMA_START_ADDR) =
  1763.                 (regsPtr->fifoData & 0xff00) >> 8;
  1764.             *(char *)(READ_DMA_ADDR(regsPtr->dmaAddress) + VMMACH_DMA_START_ADDR) =
  1765.                 (regsPtr->fifoData & 0xff);
  1766.             }
  1767. #endif
  1768. #endif
  1769.         }
  1770.  
  1771.         status =  GetStatusByte(ctrlPtr, &statusByte);
  1772.         if (status != SUCCESS) {
  1773.                     /* Return the hard error message
  1774.                      * to the call and get the next 
  1775.                      * entry in the devQueue. */
  1776.         goto rtnHardErrorAndGetNext;
  1777.         }
  1778.         RequestDone(devPtr, ctrlPtr->scsiCmdPtr, status, statusByte,
  1779.             ctrlPtr->scsiCmdPtr->bufferLen - residual);
  1780.         MASTER_UNLOCK(&(ctrlPtr->mutex));
  1781.         return(TRUE);
  1782.     }
  1783.     default: {
  1784. #ifndef SCSI3_ONBOARD
  1785.         regsPtr->control |= SI_CSR_DMA_EN;
  1786. #endif
  1787.         MASTER_UNLOCK(&(ctrlPtr->mutex));
  1788.         return(TRUE);
  1789.     }
  1790.     }
  1791.     /*
  1792.      * Jump here to return an error and reset the HBA.
  1793.      */
  1794. rtnHardErrorAndGetNext:
  1795.     RequestDone(devPtr,ctrlPtr->scsiCmdPtr,status,0,residual);
  1796.     Reset(ctrlPtr);
  1797.     MASTER_UNLOCK(&(ctrlPtr->mutex));
  1798.     return (TRUE);
  1799.  
  1800. }
  1801.  
  1802. /*
  1803.  *----------------------------------------------------------------------
  1804.  *
  1805.  * ReleaseProc --
  1806.  *
  1807.  *    Device release proc for controller.
  1808.  *
  1809.  * Results:
  1810.  *    None.
  1811.  *
  1812.  * Side effects:
  1813.  *    None.
  1814.  *
  1815.  *----------------------------------------------------------------------
  1816.  */
  1817. /*ARGUSED*/
  1818. static ReturnStatus
  1819. ReleaseProc(scsiDevicePtr)
  1820.     ScsiDevice    *scsiDevicePtr;
  1821. {
  1822.     return SUCCESS;
  1823. }
  1824.  
  1825.  
  1826. /*
  1827.  *----------------------------------------------------------------------
  1828.  *
  1829.  * DevSCSI3Init --
  1830.  *
  1831.  *    Check for the existant of the Sun SCSI3 HBA controller. If it
  1832.  *    exists allocate data stuctures for it.
  1833.  *
  1834.  * Results:
  1835.  *    TRUE if the controller exists, FALSE otherwise.
  1836.  *
  1837.  * Side effects:
  1838.  *    Memory may be allocated.
  1839.  *
  1840.  *----------------------------------------------------------------------
  1841.  */
  1842. ClientData
  1843. DevSCSI3Init(ctrlLocPtr)
  1844.     DevConfigController    *ctrlLocPtr;    /* Controller location. */
  1845. {
  1846.     int    ctrlNum;
  1847.     Boolean    found;
  1848.     Controller *ctrlPtr;
  1849.     int    i,j;
  1850.     /*
  1851.      * See if the controller is there. 
  1852.      */
  1853.     ctrlNum = ctrlLocPtr->controllerID;
  1854.     numSCSI3Controllers = ctrlNum+1;
  1855.     Controllers[0] = ctrlPtr = (Controller *) malloc(sizeof(Controller));
  1856.     ctrlPtr->regsPtr = (volatile CtrlRegs *) (ctrlLocPtr->address);
  1857. #ifdef SCSI3_ONBOARD
  1858.     {
  1859.     static char dmaTableAddress[sizeof(UDCDMAtable)];
  1860.     ctrlPtr->udcDmaTable = (UDCDMAtable *) 
  1861.         VmMach_DMAAlloc(sizeof(UDCDMAtable), dmaTableAddress);
  1862.     }
  1863. #endif
  1864.     ctrlPtr->name = ctrlLocPtr->name;
  1865.     Sync_SemInitDynamic(&(ctrlPtr->mutex),ctrlPtr->name);
  1866.     /* 
  1867.      * Initialized the name, device queue header, and the master lock.
  1868.      * The controller comes up with no devices active and no devices
  1869.      * attached.  Reserved the devices associated with the 
  1870.      * targetID of the controller (7).
  1871.      */
  1872.  
  1873.     ctrlPtr->scsiCmdPtr = (ScsiCmd *) NIL;
  1874.     Controllers[0] = ctrlPtr;
  1875.     boot_Poll = DevSCSI3Intr;
  1876.     boot_SendSCSICommand = entryAvailProc;
  1877.  
  1878.     Reset(ctrlPtr);
  1879.     return (ClientData) ctrlPtr;
  1880. }
  1881.  
  1882.  
  1883. /*
  1884.  *----------------------------------------------------------------------
  1885.  *
  1886.  * DevSCSI3AttachDevice --
  1887.  *
  1888.  *    Attach a SCSI device using the Sun SCSI3 HBA. 
  1889.  *
  1890.  * Results:
  1891.  *    None.
  1892.  *
  1893.  * Side effects:
  1894.  *    None.
  1895.  *
  1896.  *----------------------------------------------------------------------
  1897.  */
  1898.  
  1899. ScsiDevice   *
  1900. DevSCSI3AttachDevice(devicePtr, insertProc)
  1901.     Fs_Device    *devicePtr;     /* Device to attach. */
  1902.     void    (*insertProc)(); /* Queue insert procedure. */
  1903. {
  1904.     Device *devPtr;
  1905.     Controller    *ctrlPtr;
  1906.     char   tmpBuffer[512];
  1907.     int       length;
  1908.     int       ctrlNum;
  1909.     int       targetID, lun;
  1910.  
  1911.     /*
  1912.      * First find the SCSI3 controller this device is on.
  1913.      */
  1914.     ctrlPtr = Controllers[0];
  1915.     targetID = SCSI_TARGET_ID(devicePtr);
  1916.     lun = SCSI_LUN(devicePtr);
  1917.     /*
  1918.      * Allocate a device structure for the device and fill in the
  1919.      * handle part. This must be created before we grap the MASTER_LOCK.
  1920.      */
  1921.     devPtr = (Device *) malloc(sizeof(Device)); 
  1922.     devPtr->handle.LUN = lun;
  1923.     devPtr->handle.maxTransferSize =  MAX_ONBOARD_TRANSFER_SIZE;
  1924.  
  1925.     devPtr->targetID = targetID;
  1926.     devPtr->ctrlPtr = ctrlPtr;
  1927.  
  1928.     ctrlPtr->devicePtr = devPtr;
  1929.     MASTER_UNLOCK(&(ctrlPtr->mutex));
  1930.     devPtr->handle.locationName = "SCSI3";
  1931.     devPtr->handle.devQueue = (DevQueue *) devPtr;
  1932.  
  1933.     return (ScsiDevice *) devPtr;
  1934. }
  1935.  
  1936. #endif
  1937.